'use strict';

const { v4: uuidv4 } = require('uuid');

module.exports = {
  up: async (queryInterface) => {
    const now = new Date();
    
    // 先删除已存在的字典数据（强制替换）
    await queryInterface.sequelize.query(
      `DELETE di FROM dictionary_items di 
       JOIN dictionary_types dt ON di.type_id = dt.id 
       WHERE dt.code IN ('audit_log_action', 'login_status')`
    );
    await queryInterface.bulkDelete('dictionary_types', {
      code: ['audit_log_action', 'login_status']
    }, {});
    
    // 1. 创建操作日志操作类型字典类型
    const auditLogActionTypeId = uuidv4();
    await queryInterface.bulkInsert('dictionary_types', [{
      id: auditLogActionTypeId,
      code: 'audit_log_action',
      name: '操作日志类型',
      description: '系统操作日志的操作类型',
      status: true,
      created_at: now,
      updated_at: now
    }], {});

    // 2. 创建审计日志操作类型字典项
    const auditLogActions = [
      { code: 'create', label: '创建', color: 'blue', sort: 1 },
      { code: 'update', label: '更新', color: 'green', sort: 2 },
      { code: 'delete', label: '删除', color: 'red', sort: 3 },
      { code: 'login', label: '登录', color: 'cyan', sort: 4 },
      { code: 'logout', label: '登出', color: 'default', sort: 5 },
      { code: 'view', label: '查看', color: 'purple', sort: 6 },
      { code: 'export', label: '导出', color: 'orange', sort: 7 },
      { code: 'import', label: '导入', color: 'gold', sort: 8 },
      { code: 'approve', label: '审批', color: 'lime', sort: 9 },
      { code: 'reject', label: '拒绝', color: 'volcano', sort: 10 },
      { code: 'assign', label: '分配', color: 'magenta', sort: 11 },
      { code: 'upload', label: '上传', color: 'geekblue', sort: 12 },
      { code: 'download', label: '下载', color: 'cyan', sort: 13 }
    ];

    for (const action of auditLogActions) {
      await queryInterface.bulkInsert('dictionary_items', [{
        id: uuidv4(),
        type_id: auditLogActionTypeId,
        label: action.label,
        value: action.code,
        order_num: action.sort,
        status: true,
        description: action.label,
        created_at: now,
        updated_at: now
      }], {});
    }

    // 3. 创建登录状态字典类型
    const loginStatusTypeId = uuidv4();
    await queryInterface.bulkInsert('dictionary_types', [{
      id: loginStatusTypeId,
      code: 'login_status',
      name: '登录状态',
      description: '用户登录状态',
      status: true,
      created_at: now,
      updated_at: now
    }], {});

    // 创建登录状态字典项
    await queryInterface.bulkInsert('dictionary_items', [
      {
        id: uuidv4(),
        type_id: loginStatusTypeId,
        label: '登录成功',
        value: 'success',
        order_num: 1,
        status: true,
        description: '用户登录成功',
        created_at: now,
        updated_at: now
      },
      {
        id: uuidv4(),
        type_id: loginStatusTypeId,
        label: '登录失败',
        value: 'failed',
        order_num: 2,
        status: true,
        description: '用户登录失败',
        created_at: now,
        updated_at: now
      }
    ], {});
  },

  down: async (queryInterface) => {
    // 删除字典项
    await queryInterface.sequelize.query(
      `DELETE di FROM dictionary_items di 
       JOIN dictionary_types d ON di.type_id = d.id 
       WHERE d.code IN ('audit_log_action', 'login_status')`
    );
    
    // 删除字典类型
    await queryInterface.bulkDelete('dictionary_types', {
      code: ['audit_log_action', 'login_status']
    }, {});
  }
};

